<?php
class Customers extends MX_Controller
{
    function __construct()
    {
        parent::__construct();
    }
    
    function dispatcher($method = NULL, $para1 = NULL, $para2 = NULL, $para3 = NULL) {
        $main_content = NULL;
        $layout = 'layout/new_layout';
        $uri = '/' . $this->uri->uri_string();
        $options = array();
        $options['current_menu'] = $uri;

        switch ($method) {
            case 'detail_customer_file' :
                $main_content = $this->detail_customer_file(array('code' => $para1));
                break;
            case 'search_file' :
                $this->output->link_js('/scripts/site.js');
                $main_content = $this->search_file_form_mobile();
                break;
        }

        $view_data['main_menu']         = modules::run('menus/menus/get_main_menus', $options);
        $view_data['main_content'] = $main_content;

        $view_data['title'] = $this->_title;
        $view_data['keywords'] = $this->_keywords;
        $view_data['description'] = $this->_description;
        if (MOBILE_SITE) {
            $view_data['main_menu']          = modules::run('menus/menus/get_mobile_main_menus', $options);
            $layout = 'mobile/content_layout';
        }
        $this->load->view($layout, $view_data);
    }

    function search_file_form()
    {
        $view_data = array();
        return $this->load->view('search_file_form', $view_data, TRUE);
    }
    
    function search_file_form_mobile()
    {
        $view_data = array();
        return $this->load->view('mobile/search_file_form', $view_data, TRUE);
    }
    
    function search_file()
    {
        $options                = array();
        $options['lg_code']     = trim($this->input->post('code', TRUE));
        $options['password']    = md5(trim($this->input->post('password', TRUE)));

        $customers = $this->customers_model->get_customers($options);
        if (count($customers) == 1)
        {
            $customer = $customers[0];
            if (($options['lg_code'] === trim($customer->code))
             && (($options['password']) === trim($customer->password))
             && ($customer->active == 1))
            {
                $this->phpsession->save('customer_id', $customer->id);
                echo 'ok';
            }
            else
            {
                echo 'not_ok';
            }
        }
        else
        {
            echo 'not_ok';
        }
    }
    
    function detail_customer_file($options = array())
    {
        $customers = $this->customers_model->get_customers($options);
        if(count($customers) == 0) return show_404();
        $customer = $customers[0];
        if($this->phpsession->get('customer_id') != $customer->id) return show_404();
        $view_data = array();
        $view_data['customers_files']   = $this->customers_model->get_customer_files(array('customer_id' => $customer->id));
        $view_data['customer_name']     = $customer->name;
        return $this->load->view('detail_customer_file', $view_data, TRUE);
    }
    
    function select_customer_file()
    {
        $options        = array();
        $options['id']  = $this->input->post('id');
        $view_data['customers_file']   = $this->customers_model->get_customer_files($options);
        echo $this->load->view('select_files_ajax_content', $view_data, TRUE);
    }
}
?>
